from app.system import systembp
from flask import request
from app.utils.dbFunc import MySQLClinet as mysql
from app.utils.requestParse import Parse
from app.utils.responseFunc import resultMsg


@systembp.route("/userMange/list",methods=["post"])
def userList():
    """
    @功能    :获取管理员列表
    @参数    :
    @返回值  :
    @时间    :2022/11/06 16:41:39
    @作者    :浪晋
    @版本    :1.0
    """
    body = request.get_json()
    realName = body.get("realName")
    account = body.get("account")
    telPhone = body.get("telPhone")
    pageSize = body.get("pageSize")
    pageNum = body.get("pageNum")
    parse = Parse()
    parse.parseRule(value=pageSize,valueName="每页显示数目",required=True,regexp="^([1-9][0-9]*)$")
    parse.parseRule(value=pageNum,valueName="页码",required=True,regexp="^([1-9][0-9]*)$")
    result = parse.checkRule()
    if result != True:
        return resultMsg(code=0,msg=result)
    if pageNum == 1:
        pageNum = 0
    else:
        pageNum = (pageNum - 1) * pageSize
    sql = "select * from tb_system_user where status != 0 "
    csql = "select count(*) total from tb_system_user where status != 0 "
    if account not in ("",None):
        sql = sql + " and account like '%{}%'".format(account)
        csql = csql + " and account like '%{}%'".format(account)
    if realName not in ("",None):
        sql = sql + " and realName like '%{}%'".format(realName)
        csql = csql + " and realName like '%{}%'".format(realName)
    if telPhone not in ("",None):
        sql = sql + " and telPhone like '%{}%'".format(telPhone)
        csql = csql + " and telPhone like '%{}%'".format(telPhone)
    sql = sql + " order by createTime desc limit {},{};".format(pageNum,pageSize)
    csql = csql + " order by createTime desc;"
    dbRes = mysql().query(sql)
    cdbRes = mysql().query(csql)
    return resultMsg(data=dbRes,total=cdbRes[0].get("total"))


@systembp.route("/userMange/add",methods=["post"])
def addUser():
    """
    @功能    :新增用户
    @参数    :
    @返回值  :
    @时间    :2022/11/06 16:41:39
    @作者    :浪晋
    @版本    :1.0
    """
    body = request.get_json()
    headImg = body.get("headImg")
    account = body.get("account")
    password = body.get("password")
    realName = body.get("realName")
    parse = Parse()
    parse.parseRule(value=headImg,valueName="头像")
    parse.parseRule(value=account,valueName="账号",required=True)
    parse.parseRule(value=password,valueName="密码",required=True)
    parse.parseRule(value=realName,valueName="姓名",required=True)
    result = parse.checkRule()
    if result != True:
        return resultMsg(code=0,msg=result)
    sql = "insert into tb_system_user (account,password,realName) values ('{}','{}','{}');".format(account,password,realName)
    dbMsg = mysql().commit(sql)
    if dbMsg == False:
        return resultMsg(code=0,msg="系统异常")
    return resultMsg()


@systembp.route("/userMange/edit",methods=["post"])
def editUser():
    """
    @功能    :修改用户
    @参数    :
    @返回值  :
    @时间    :2022/11/06 16:41:39
    @作者    :浪晋
    @版本    :1.0
    """
    body = request.get_json()
    uid = body.get("id")
    headImg = body.get("headImg")
    realName = body.get("realName")
    sex = body.get("sex")
    status = body.get("status")
    parse = Parse()
    parse.parseRule(value=uid,valueName="ID",required=True)
    parse.parseRule(value=headImg,valueName="头像",required=True)
    parse.parseRule(value=realName,valueName="姓名",required=True)
    parse.parseRule(value=sex,valueName="性别",required=True)
    parse.parseRule(value=status,valueName="状态",required=True)
    result = parse.checkRule()
    if result != True:
        return resultMsg(code=0,msg=result)
    sql = "update tb_system_user set headImg = '{}',realName='{}',sex='{}',status='{}' where id = {};".format(headImg,realName,sex,status,uid)
    dbMsg = mysql().commit(sql)
    if dbMsg == False:
        return resultMsg(code=0,msg="系统异常")
    return resultMsg()

@systembp.route("/userMange/del",methods=["post"])
def delUser():
    """
    @功能    :删除用户
    @参数    :
    @返回值  :
    @时间    :2022/11/06 16:41:39
    @作者    :浪晋
    @版本    :1.0
    """
    body = request.get_json()
    uid = body.get("id")
    parse = Parse()
    parse.parseRule(value=uid,valueName="ID",required=True)
    result = parse.checkRule()
    if result != True:
        return resultMsg(code=0,msg=result)
    sql = "update tb_system_user set status=0 where id = {};".format(uid)
    dbMsg = mysql().commit(sql)
    if dbMsg == False:
        return resultMsg(code=0,msg="系统异常")
    return resultMsg()